﻿//----------------------------------------------------------------
// Copyright (c) 2011-2013 
// All rights reserved.
// Author: Ben DaoWei 
// Contact: bendaowei@gmail.com
// Web Link: http://xnaeditor.codeplex.com
// ----------------------------------------------------------------


namespace UnityEngine.Components
{
    using System.IO;
    using Microsoft.Xna.Framework;
    using UnityEngine.Components;
    using UnityEngine.Core;
    using UnityEngine.Graphics;
    using System.Xml.Serialization;
    using System.Xml;
    using UnityEngine.IO;
    using System.Diagnostics;
    using System.ComponentModel;
    using System;
    using UnityEngine.Gameplay;
    using UnityEngine.Gameplay.Match;

    public class Renderer : UnityEngine.Core.Component
    {
        public RendererInstance Instance
        {
            get;
            set;
        }

        public Renderer()
            : this(null)
        {
        }

        public Renderer(GameObject gameObject)
            : base(gameObject)
        {
        }


        private string modelPath = null;
        [Editable]
        public string ModelPath
        {
            get
            {
                modelPath = IOManager.Instance.GetAssertPath(modelPath);
                return modelPath;
            }
            set
            {
                modelPath = value;
                modelPath = IOManager.Instance.GetAssertPath(modelPath);
            }
        }

        private bool visibility = true;
        [Editable]
        public bool Visibility
        {
            get
            {
                return visibility;
            }
            set
            {
                visibility = value;
            }
        }

        public string Key
        {
            get
            {
                return Path.GetFileName(this.ModelPath);
            }
        }

        public bool IsValid
        {
            get
            {
                return this.ModelPath != null && this.ModelPath.Length > 0 && this.Key.Length > 0;
            }
        }

        public ModelRenderer ModelRenderer { get; set; }

        public override void Startup(Microsoft.Xna.Framework.GameTime time)
        {
            this.ModelRenderer = SceneManager.Instance.PaletteRendererManager.GetPaletteRenderer(EPaletteRendererType.Custom, this.ModelPath) as ModelRenderer;
            Instance = new RendererInstance(this.GameObject.GetComponent<Transform>(), this.ModelRenderer, Vector3.Zero, Vector3.One);
            Instance.Renderer = this;
            Instance.IsMovable = true;
            Instance.IsCreateShadow = false;
            Instance.TeamType = this.GameObject.GetParent<Team>().TeamRelationType;
            SceneManager.Instance.AddInstance(Instance);

            Debug.Assert(this.ModelRenderer != null);

            base.Startup(time);
        }

        public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
        {
            if (this.IsValid == false || this.Visibility == false)
            {
                Instance.IsInBoundingFrustum = false;
                return;
            }

            Transform transformer = this.GameObject.GetComponent<Transform>();
            Instance.Position = transformer.Position;
        }

        public override void Destroy(GameTime gameTime)
        {
            SceneManager.Instance.RemoveInstance(Instance);

            base.Destroy(gameTime);
        }

        public override int Priority
        {
            get
            {
                return 10;
            }
        }
    }
}
